package com.kitchen.ui.bg
{
	import com.kitchen.http.DecorateProxy;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;

	internal class BackDeco extends Sprite
	{
		public var deco:Object;
		private var edit:Boolean;
		private var loader:Loader;
		private static var decoProxy:DecorateProxy = null;
		public function BackDeco(deco:Object)
		{
			super();
			this.deco = deco;
			this.edit = false;
			this.layout();
		}
		
		public function setEdit():void{
			this.edit = true;
			this.buttonMode = true;
			this.useHandCursor = true;
		}
		
		public function cancelEdit():void{
			this.edit = false;
			this.buttonMode = false;
			this.useHandCursor = false;
		}
		
		private function layout():void{
			this.loader = new Loader();
			this.loader.load(new URLRequest(this.deco.sUrl));
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHandler);
			this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			this.addChild(this.loader);
			this.x = this.deco.nPx;
			this.y = this.deco.nPy;
			this.addEventListener(MouseEvent.MOUSE_DOWN, mdownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP, mupHandler);
		}
		
		private function mdownHandler(event:MouseEvent):void{
			this.startDrag();
		}
		
		private function mupHandler(event:MouseEvent):void{
			if(0 < this.deco.id){
				 
			}else{
				this.dispatchEvent(new Event('buydeco'));
			}
		}
		
		private function loadedHandler(event:Event):void{
			this.loader.removeEventListener(Event.COMPLETE, loadedHandler);
		}
		
		private function errorHandler(event:IOErrorEvent):void{
			trace('load error:'+this.deco.sUrl);
		}
		
		private function getDecoProxy():DecorateProxy{
			if(BackDeco.decoProxy == null){
				BackDeco.decoProxy = new DecorateProxy();
			}
			return BackDeco.decoProxy;
		}
	}
}